<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="" prop="userId">
        <el-input v-model="dataForm.userId" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="memberId">
        <el-input v-model="dataForm.memberId" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="申请时间" prop="applyTime">
        <el-input v-model="dataForm.applyTime" placeholder="申请时间"></el-input>
      </el-form-item>
      <el-form-item label="申请审批状态  0：审批拒绝   1：审批通过" prop="status">
        <el-input v-model="dataForm.status" placeholder="申请审批状态  0：审批拒绝   1：审批通过"></el-input>
      </el-form-item>
      <el-form-item label="申请审批时间" prop="applyedTime">
        <el-input v-model="dataForm.applyedTime" placeholder="申请审批时间"></el-input>
      </el-form-item>
      <el-form-item label="申请审批内容" prop="applyedDesc">
        <el-input v-model="dataForm.applyedDesc" placeholder="申请审批内容"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          memberId: '',
          applyTime: '',
          status: '',
          applyedTime: '',
          applyedDesc: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          memberId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          applyTime: [
            { required: true, message: '申请时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '申请审批状态  0：审批拒绝   1：审批通过不能为空', trigger: 'blur' }
          ],
          applyedTime: [
            { required: true, message: '申请审批时间不能为空', trigger: 'blur' }
          ],
          applyedDesc: [
            { required: true, message: '申请审批内容不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.basememberapply.info(this.dataForm.id).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.basememberapply.userId
                this.dataForm.memberId = data.basememberapply.memberId
                this.dataForm.applyTime = data.basememberapply.applyTime
                this.dataForm.status = data.basememberapply.status
                this.dataForm.applyedTime = data.basememberapply.applyedTime
                this.dataForm.applyedDesc = data.basememberapply.applyedDesc
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'userId': this.dataForm.userId,
              'memberId': this.dataForm.memberId,
              'applyTime': this.dataForm.applyTime,
              'status': this.dataForm.status,
              'applyedTime': this.dataForm.applyedTime,
              'applyedDesc': this.dataForm.applyedDesc
            }
            var tick = !this.dataForm.id ? API.basememberapply.add(params) : API.basememberapply.update(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
